package cn.iocoder.yudao.module.bill.controller.admin.account;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.bill.controller.admin.account.vo.*;
import cn.iocoder.yudao.module.bill.dal.dataobject.account.AccountDO;
import cn.iocoder.yudao.module.bill.service.account.AccountService;

@Tag(name = "管理后台 - 账户信息")
@RestController
@RequestMapping("/bill/account")
@Validated
public class AccountController {

    @Resource
    private AccountService accountService;

    @PostMapping("/create")
    @Operation(summary = "创建账户信息")
    @PreAuthorize("@ss.hasPermission('bill:account:create')")
    public CommonResult<Long> createAccount(@Valid @RequestBody AccountSaveReqVO createReqVO) {
        return success(accountService.createAccount(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新账户信息")
    @PreAuthorize("@ss.hasPermission('bill:account:update')")
    public CommonResult<Boolean> updateAccount(@Valid @RequestBody AccountSaveReqVO updateReqVO) {
        accountService.updateAccount(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除账户信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('bill:account:delete')")
    public CommonResult<Boolean> deleteAccount(@RequestParam("id") Long id) {
        accountService.deleteAccount(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得账户信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('bill:account:query')")
    public CommonResult<AccountRespVO> getAccount(@RequestParam("id") Long id) {
        AccountDO account = accountService.getAccount(id);
        return success(BeanUtils.toBean(account, AccountRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得账户信息分页")
    @PreAuthorize("@ss.hasPermission('bill:account:query')")
    public CommonResult<PageResult<AccountRespVO>> getAccountPage(@Valid AccountPageReqVO pageReqVO) {
        PageResult<AccountDO> pageResult = accountService.getAccountPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AccountRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出账户信息 Excel")
    @PreAuthorize("@ss.hasPermission('bill:account:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportAccountExcel(@Valid AccountPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<AccountDO> list = accountService.getAccountPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "账户信息.xls", "数据", AccountRespVO.class,
                        BeanUtils.toBean(list, AccountRespVO.class));
    }

}